/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

package com.worklight.wlclient.api;

import org.apache.http.HttpResponse;

import com.worklight.wlclient.WLClientUtils;

/**
 * Passed as an argument to the WLClient invokeProcedure delegate methods. 
 */
public class WLResponse {
	protected int status;	
	protected WLRequestOptions requestOptions; 
	protected String responseText;
	
	public WLResponse(HttpResponse httpResponse) {
		this.status = httpResponse.getStatusLine().getStatusCode();
		try {
			this.responseText = WLClientUtils.convertStreamToString(httpResponse.getEntity().getContent());
		} catch (Exception e) {
			WLClientUtils.error("Exception while try to read response body", e);
		}
	}
	
	public WLResponse(WLResponse response) {
		this.status = response.status;		
		this.requestOptions = response.requestOptions;
		this.responseText = response.responseText;
	}
	
	protected WLResponse(int status, String responseText, WLRequestOptions requestOptions) {
		this.status = status; 
		this.requestOptions = requestOptions;
		this.responseText = responseText;
	}
	
	/**
	 * @return the http status
	 */
	public int getStatus() {
		return status;
	}
		
	/**
	 * @return the user invocation context
	 */
	public Object getInvocationContext() {
		return requestOptions.getInvocationContext();
	}
	
	/**
	 * @return the http response text
	 */
	public String getResponseText() {
		return responseText;
	}
	
	public void setInvocationContext(Object invocationContext) {
		requestOptions.setInvocationContext(invocationContext);
	}
	
	public WLRequestOptions getOptions() {
		return requestOptions;
	}

	public void setOptions(WLRequestOptions wlOptions) {
		this.requestOptions = wlOptions;
	}
				
	@Override
	public String toString() {
		return "WLResponse [invocationContext=" + requestOptions.getInvocationContext() + 
				", responseText=" + responseText + 
				", status=" + status + "]";
	}
}
